<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../header.jsp"%>

<style type="text/css">
.stats_total {
	font-weight: bold;
}
</style>

<div class="border bg">
<div class="page-header">
	<h2>任务统计<small>(<span id="currentCount">0</span>/<span id="totalCount">0</span>)</small></h2>
	<div class="cms_toolbar">
		<a id="exportButton" href="#" onclick="exportExcel()" class="btn"><i class="icon-share"></i>导出</a>
	</div>
</div>

<div class="text-right form-inline">
	<label class="control-label" for="locateStartDate">&nbsp;定位时间从：</label>
	<input type="text" name="locateStartDate" id="locateStartDate" class="input-small" readonly="readonly" value="${locateStartDate}" />
	<label class="control-label" for="locateEndDate">到</label>
	<input type="text" name="locateEndDate" id="locateEndDate" class="input-small" readonly="readonly" value="${locateEndDate}" />	
	<label class="control-label" for="executor">&nbsp;部门/人员：</label>
	<input type="text" name="executor" id="executor" class="input-small" readonly="readonly" onclick="selectExecutor()" placeholder="请选择部门或人员..."/>
	
	<button class="btn" onclick="refresh()">查询</button>
</div>

<div>
	<br />
	<table id="listTable" class="table table-striped cms-table">
		<thead>
			<tr>
				<th>部门</th>
				<th width="80">人员</th>
				<th width="150">区域</th>
				<th width="100">出勤天数</th>
				<th width="100">完成任务数</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	<div class="well cms_more">
		<a href="javascript:;" onclick="moreData()"><i class="icon-arrow-down"></i>查看更多...</a>
	</div>
</div>

</div>

<table id="taskTemplate" class="hide">
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

<table id="taskFirstTemplate" class="hide">
  <tr>
    <td rowspan="2"></td>
    <td rowspan="2"></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>


<script type="text/javascript">
var pageIndex = 0, pageSize = 10, currentCount = 0, totalCount = 0;

function getParameter(){
	return {
		start: pageIndex,
		limit: pageSize,
		execPersonId: $("#executor").data("type") == "user" ? $("#executor").data("id") : "",
		execOrganId: $("#executor").data("type") == "organ" ? $("#executor").data("id") : "",
		locateStartDate: $("#locateStartDate").val(),
		locateEndDate: $("#locateEndDate").val(),
		name: $("#name").val()
	};
}

function exportExcel(){
	if(totalCount == 0) {
		$.alert("您尚未查出数据，无法使用导出功能。");
		return;
	}
	var url = "statistics/excel.do?status=completed&locateStartDate=" + $("#locateStartDate").val();
	url = url + "&locateEndDate=" + $("#locateEndDate").val();
	if($("#executor").data("type") == "user"){
		url = url + "&execPersonId=" + $("#executor").data("id");
	}
	if($("#executor").data("type") == "organ"){
		url = url + "&execOrganId=" + $("#executor").data("id");
	}
	window.open(url);
}

function count(){
	block(".cms_more", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/task/statistics/count.json",
    	data: getParameter(),
    	dataType:"json",
    	success:function(json) {
    		unblock(".cms_more");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
    		totalCount = json.data;
			$("#totalCount").text(json.data);
			list();
    	},
		error: function(message) {
			unblock(".cms_more");
			$.alert(message);
		}
	});
}

function list(){
	block(".cms_more", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/task/statistics/list.json",
    	data: getParameter(),
    	dataType:"json",
    	success:function(json) {
    		unblock(".cms_more");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
			buildList(json.data);
    	},
		error: function(message) {
			unblock(".cms_more");
			$.alert(message);
		}
	});
}

function refresh(){
	pageIndex = 0;
	currentCount = 0;
	totalCount = 0;
	pageSize = 10;
	
	$("#currentCount").text(currentCount);
	$("#totalCount").text(totalCount);
	$("#listTable").find("tbody").html("");
	$(".cms_more").show();
	count();
	//list();
}

function moreData(){
	pageIndex++;
	list();
}

function buildList(data){
	if(data.length == 0){
		$(".cms_more").hide();
		return;
	}
	currentCount += data.length;
	$("#currentCount").text(currentCount);
	if(currentCount >= totalCount){
		$(".cms_more").hide();
	} else {
		$(".cms_more").show();
	}
	$(data).each(function(index, item) {
		buildTd(item);
	});
}

function buildTd(item){
	var statses = item.stats;
	for(var i = 0; i < statses.length; i++){
		var stats = statses[i];
		var taskEle;
		if(i == 0){
			taskEle = $("#taskFirstTemplate").clone();
			var tdList = taskEle.find("td");
			$(tdList.get(0)).attr("rowspan", statses.length)
			$(tdList.get(0)).text(item.organName);
			$(tdList.get(1)).attr("rowspan", statses.length)
			$(tdList.get(1)).text(item.name);
			$(tdList.get(2)).text(stats.county);
			$(tdList.get(3)).text(stats.dateCount);
			$(tdList.get(4)).text(stats.taskCount);
		} else {
			taskEle = $("#taskTemplate").clone();
			var tdList = taskEle.find("td");
			$(tdList.get(0)).text(stats.county);
			$(tdList.get(1)).text(stats.dateCount);
			$(tdList.get(2)).text(stats.taskCount);
			if(i == statses.length -1){
				tdList.addClass("stats_total");
			}
		}
		taskEle.find("tr").appendTo($("#listTable").find("tbody"));
	}
}

function selectExecutor() {
	$.openDialog("#dialog",
			{height:350,
			width:300,
			minHeight:350,
			minWidth:300,
			loadPage:ctx + "/biz/system/user/tree_single.jsp",
			title:"选择部门或人员"},
			function(data) {
				$("#executor").data("id",data.data("id"));
				$("#executor").data("type",data.data("type"));
				$("#executor").val(data.data("name"));
			});
}


$(document).ready(function() {
	$("#locateStartDate").datepicker();
	$("#locateEndDate").datepicker();
	
	refresh();
});
</script>

<%@ include file="../../footer.jsp"%>